<template>
  <div class='container'>
    <el-dialog title="新增目录" :visible="showDialog" width="400px" top="16vh" @close="closeDialog" @open="openDialog">
        <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="所属学科" prop="subjectID" v-if="!$route.query.id">
            <el-select v-model="form.subjectID" style="width:100%" placeholder="请选择">
              <el-option
              v-for="item in optionsList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="tagName">
            <el-input v-model="form.tagName" style="width:100%" placeholder="请输入目录名称"></el-input>
          </el-form-item>
        </el-form>
        <template v-slot:footer>
          <el-button  @click="closeDialog">取消</el-button>
          <el-button type="primary" @click="addTags">确定</el-button>
        </template>
        </el-dialog>
  </div>
</template>

<script>
import { add } from '@/api/hmmm/tags'
import { simple } from '@/api/hmmm/subjects.js'
export default {
  name: 'AddTags',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      optionsList: [],
      form: {
        subjectID: '',
        tagName: ''
      },
      rules: {
        subjectID: [
          { required: true, message: '学科不能为空', trigger: ['blur', 'change'] }
        ],
        tagName: [
          { required: true, message: '目录名称不能为空', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    /**
     * 确定按钮
     */
    addTags () {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.$route.query.id) {
          this.form.subjectID = Number(this.$route.query.id)
        }
        await add(this.form)
        this.$message.success('添加成功')
        this.closeDialog()
        this.$parent.listApi()
      })
    },
    closeDialog () {
      this.$refs.addForm.resetFields()
      this.subjectID = ''
      this.tagName = ''
      this.$emit('update:showDialog', false)
    },
    /**
     * 获取学科列表
     */
    async openDialog () {
      const { data } = await simple()
      // console.log(data)
      this.optionsList = data
    }
  }
}
</script>

<style scoped lang='less'></style>
